module asinus
import StdEnv, japi

/* This example shows how to transform plots from real
to integer coordinates. The csinus example shows an
easier way to achieve the same goal. */

:: Stt = {fr :: !Framer, cv :: !Canvas, mouse :: !Device}

Start world
	#	(js, world) = begin_japi world
		(frm, js) = jnewFrame (WH 500 500) "Curto" js
		(cvs, js) = jnewCanvas (XYWH 0 0 500 500) frm js
		(mp, cvs) = jmousepressed cvs 
		(frm, js) = jshow frm js
		hlp= "Click anywhere on the Canvas."
		cvs= draw [ Txt (50, 60) hlp] cvs
		s = { fr = frm, cv = cvs, mouse = mp}
		js = loop s js
	= end_japi js world
where
	loop :: !Stt  !JS -> JS
	loop s js 
		#	(na, js) = jnextaction js
		| na == act s.fr = jquit js
		| na == act s.mouse 
		        # (x,y)= mxy s.mouse
				  cvs = sinus 0.0 (2.0*pi) (x, y) s.cv
				= loop {s & cv = cvs} js
		= loop s js

pi= 4.0 * (atan 1.0)

x2c :: (Int, Int) Int Int (Real, Real) -> (Int, Int)
x2c (x0, y0) w h p
  # mx= (toReal w)/10.0
    xc= toInt (mx * p@X) + x0
    my= (~(toReal h))/4.0
    yc= toInt(my * p@Y) + y0
  = (xc, yc) 

sinus i f p cvs
  # (w, cvs)= jgetwidth cvs
  # (h, cvs)= jgetheight cvs
  = draw [ Hue red,
           Pll [x2c p w h (x, sin x) \\
                 x <- [i, i+0.1..f]]] cvs

